package com.steven.treatment_nurse.fragment;

import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;

import com.steven.treatment_nurse.R;
import com.steven.treatment_nurse.entity.Constant;
import com.tencent.smtt.export.external.interfaces.SslErrorHandler;
import com.tencent.smtt.export.external.interfaces.WebResourceRequest;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import butterknife.BindView;
import pl.droidsonroids.gif.GifImageView;

@SuppressLint("ValidFragment")
public class BodyTFragment extends LazyLoadBaseFragment {
    private String batchId;
    @BindView(R.id.webView)
    WebView webView;
    @BindView(R.id.gif)
    GifImageView gifImageView;

    public BodyTFragment(String batchId) {
        this.batchId = batchId;
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_bodyt;
    }

    @Override
    protected void initView(View rootView, Bundle savedInstanceState) {

    }

    @Override
    protected void requestData() {
        super.requestData();
        requestDataAutoRefresh();
    }

    @Override
    protected void requestDataAutoRefresh() {
        super.requestDataAutoRefresh();
        setWebView();
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void setWebView() {

        WebSettings settings = webView.getSettings();

        //WebView加载web资源
        int screenDensity = getResources().getDisplayMetrics().densityDpi;
        WebSettings.ZoomDensity zoomDensity = WebSettings.ZoomDensity.MEDIUM;
        switch (screenDensity) {
            case DisplayMetrics.DENSITY_LOW:
                zoomDensity = WebSettings.ZoomDensity.CLOSE;
                break;
            case DisplayMetrics.DENSITY_MEDIUM:
                zoomDensity = WebSettings.ZoomDensity.MEDIUM;
                break;
            case DisplayMetrics.DENSITY_HIGH:
                zoomDensity = WebSettings.ZoomDensity.FAR;
                break;
            default:
                break;
        }
        settings.setDefaultZoom(zoomDensity);

        settings.setSupportZoom(true);
        settings.setBuiltInZoomControls(true);
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);// 不使用缓存
//        webView.addJavascriptInterface(new JsInterface(WebActivity.this), "android");
        String ua = settings.getUserAgentString();
        settings.setUserAgentString(ua + "android/ts");
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);//把html中的内容放大webview等宽的一列中
        settings.setAppCacheEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setDisplayZoomControls(false);//隐藏Zoom缩放按钮
        //解除数据阻止
        settings.setBlockNetworkImage(false);

        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
            }

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    if (gifImageView != null)
                        gifImageView.setVisibility(View.GONE);
                } else {
                    if (gifImageView != null)
                        gifImageView.setVisibility(View.VISIBLE);
                }
                super.onProgressChanged(view, newProgress);
            }
        });
        //覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为，使网页用WebView打开
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                //网页在webView中打开
                if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP) {//安卓5.0的加载方法
                    view.loadUrl(request.toString());
                } else {//5.0以上的加载方法
                    view.loadUrl(request.getUrl().toString());
                }
                return true;
            }

            @Override
            public void onReceivedSslError(WebView webView, SslErrorHandler sslErrorHandler, com.tencent.smtt.export.external.interfaces.SslError sslError) {
                sslErrorHandler.proceed();  // 接受所有网站的证书
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
            }
        });
        webView.loadUrl(Constant.temperature + batchId);
    }
}
